import { Component, OnInit, Output, EventEmitter, ElementRef, ViewChild } from '@angular/core';

import { InitService } from 'app/public/serve/init/init.service';
import { CudTableService } from 'app/public/serve/cud-table/cud-table.service';
import { HintsService } from 'app/public/serve/hints/hints.service';
import { Service } from '../../../server/server';
import { sysconfig } from '../sysconfig.data';
import { sysconfigUrl } from '../sysconfig.url';

declare var $: any;

@Component({
  selector: 'sysconfig-newly-added',
  templateUrl: './sysconfig-newly-added.component.html',
  styleUrls: ['./sysconfig-newly-added.component.css']
})
export class SysconfigNewlyAddedComponent implements OnInit {
  
  @Output() onSubmit = new EventEmitter<void>();

  constructor(
    public initService: InitService,
    private request: CudTableService,
    private hintsService: HintsService,
    public server: Service,
  ) {}

  ngOnInit() {
  }

  public initial:boolean = true;

  /**
   * 提交新增
   * @param info 
   */ 
  _onSubmit(info: any) { 
    info.sysStatus =  Number(info.sysStatus);
    this.request
        .postRequest(this.server.baseUrl+sysconfigUrl.add, info)
        .then(res=>{
          if (res.result_code == '0000') {
              this.onSubmit.emit();
            }
            this.hintsService.messages(res.message, res.result_code);
            this._init();
            return
        }).catch(err=>{
          this.hintsService.messages('error', 'error');
          this._init();
          return
        })
  }

  /**
   * 初始化弹窗
   */
  private _init() {
    this.initService._init();
    $('#newlyAdded').modal('hide');
  } 

}
